//供其他页面调用声明
function __g_reloadData(){
	$(".datagrid:visible .tablex").treegrid("reload");
}


function edit(node,event){
	var url = $(node).data("url");
	top.__t_tabAdd("修改部门",url,window.name);
	event.stopPropagation();
};


function before(){

}

function init(){
	$('#departmentTree').treegrid({
		"url" : "sys/department/queryTree",
		"columns" : [ [ {
			title : '名称',
			field : 'name',
			width : 280
		}, {
			title : '创建时间',
			field : 'createDate',
			width : 160,
			align : 'center'
		} ] ],
		"toolbar": [{
			iconCls: 'fa fa-plus',
			text:"增加",
			handler: function(){
				var node = $("#departmentTree").treegrid("getSelected");
				var parentId = "";
				if(node != null){
					parentId = node.id;
				}
				if(parentId==""||parentId==null){
					$.messagebox.confirm("将会创建根级部门，是否继续？",function(res){
						if(res){
							top.__t_tabAdd("新增部门", "sys/department/add?parentId=", window.name );
						}
					})
				}else{
					top.__t_tabAdd("新增部门", "sys/department/add?parentId="+parentId, window.name );
				}
			}
		},"-",{
			iconCls: 'fa fa-edit',
			text:"修改",
			handler: function(){
				var node = $("#departmentTree").treegrid("getSelected");
				if(node == null){
					$.messagebox.toast("请选择部门","warning");
					return;
				}
				var nodeId = node.id;
				top.__t_tabAdd("修改部门", "sys/department/edit?id="+nodeId, window.name );
			}
		},"-",{
			iconCls: 'fa fa-recycle',
			text:"删除",
			handler: function(){
				var node = $("#departmentTree").treegrid("getSelected");
				if(node == null){
					$.messagebox.toast("请选择部门","warning");
					return;
				}
				var nodeId = node.id;
				$.messagebox.confirm3("是否删除子部门？",function(res){
					if(res==true){
						$.post("sys/department/delete",{
							"id":nodeId,
							"deleteChild":true
						},function(response){
							if (response.success) {
								$.messagebox.toast("删除部门成功", "success");
								$("#departmentTree").treegrid("unselect",nodeId);
								__g_reloadData();
							} else {
								$.messagebox.toast("删除部门失败", "error");
							}
						}).error(function(){alert("系统异常")});
					}else if(res==false){
						$.post("sys/department/delete",{
							"id":nodeId,
							"deleteChild":false
						},function(response){
							if (response.success) {
								$.messagebox.toast("删除部门成功", "success");
								$("#departmentTree").treegrid("unselect",nodeId);
								__g_reloadData();
							} else {
								$.messagebox.toast("删除部门失败", "error");
							}
						}).error(function(){alert("系统异常")});
					}
				})
			}
		},"-"],
		"onLoadSuccess" : function(row) {
			$(this).treegrid('enableDnd', row ? row.id : null);
		},
		"onDrop" : function(targetRow, sourceRow, point) {
			$.messagebox.confirm("是否更改部门节点", function(res) {
				if (res) {
					var childId = sourceRow.id;
					var parentId = targetRow.id;
					if (point != "append") {
						parentId = targetRow._parentId;
					}

					$.post("sys/department/changeTreeNode", {
						"childId" : childId,
						"parentId" : parentId
					}, function(response) {
						if (response.success) {
							$.messagebox.toast("更改部门节点成功", "success");
						} else {
							$.messagebox.toast("更改部门节点失败", "error");
						}
					}).error(function(){
						alert("系统错误");
					});
				} else {
					$('#departmentTree').treegrid("reload");
				}
			});
		}
	});
	

	__p_resizeable($(".tablex"));

};


function after(){
	
}